<?php
namespace apps\admin\controller;
use apps\common\controller\Backend;
use apps\common\model\Menu;

class Menus extends Backend
{
    protected $mod = null;

	protected static function getMenuList($cate , $lefthtml = '——' , $pid=0 , $lvl=0, $leftpin=0){
		$arr = [];
		foreach ($cate as $k=>$v){
			if($v['pid']==$pid){
				$v['lvl']=$lvl + 1;
				$v['leftpin']=$leftpin + 0;//左边距
				$v['lefthtml']=str_repeat($lefthtml,$lvl);
				$arr[]=$v;
				$arr = array_merge($arr,self::getMenuList($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+20));
			}
		}
		return $arr;
	}
    /** 菜单列表 */
    public function index()
    {
        if(is_ajax()){
			$where = [];
			$uname = input('title');
			if(!empty($uname)) $where['title'] = ['like','%'.$uname.'%'];
			
			// 分页
			//$nowpage = input('get.page') ? input('get.page') : 1;
			//$limit = input('get.limit') ? input('get.limit') : 10;// 获取总条数
			//$count =  db('admin_menu')->where($where)->count();//计算总页面

			//查询
			//$lists = db('admin_menu')->page($nowpage, $limit)->where($where)->select();
			$lists = db('admin_menu')->where($where)->select();  			
			$lists = self::getMenuList($lists);
			foreach($lists as $k=>$val){
				$lists[$k]['title'] = $val['lefthtml'].$val['title'];
			}   			
			//return json(['code'=>0, 'msg'=>'success', 'count'=>$count, 'data'=>$lists]);
			return json(['code'=>0, 'msg'=>'success', 'data'=>$lists]);
        }
        return view('index');
    }
    
    /** 创建菜单 */
	public function add(){
        if(is_ajax()){
	        $exits = db('admin_menu')->where(['name'=>input('name')])->find();
	        if($exits){
	            return $this->renderError('该菜单URL已经存在；请更换');
	        }
	        $data = [
                'pid' => input('pid'),
	            'name' => input('name'),
	            'title' => input('title'),
	            'sort' => input('sort'),
	            'css' => input('css'),
                'create_time' => time(),
                'update_time' => time(),
	            'status' => input('status')=='on'?1:0,
	        ];
	        $ret = db('admin_menu')->insert($data);
	        if($ret){
	            return $this->renderSuccess([],'保存成功');
	        }
	        return $this->renderError('保存失败');
	    }
		$lists = db('admin_menu')->where(['status'=>1])->select();
		$lists = self::getMenuList($lists);
        return view('',['lists'=>$lists]);
    }
            
    /** 删除菜单 */
	public function del(){
		if(is_ajax()){			
			$id = input('id');
			$exits = db('admin_menu')->where("id IN ({$id})")->delete();
			if($exits){
				return $this->renderSuccess([],'删除成功');
			}
			return $this->renderError('删除失败');
		}
    }
    
    /** 更新菜单 */
	public function edit(){
		if(is_ajax()){
			$id = input('id');
			$uname = input('name');
	        $exits = db('admin_menu')->where("name='{$uname}' and name<>'#' and id<>'{$id}'")->find();
	        if($exits){
	            return $this->renderError('该菜单URL已经存在；请更换');
	        }
	        $data = [
                'pid' => input('pid'),
	            'name' => input('name'),
	            'title' => input('title'),
	            'sort' => input('sort'),
	            'css' => input('css'),
                'update_time' => time(),
	            'status' => input('status')=='on'?1:0,
	        ];
	        $ret = db('admin_menu')->where("id='{$id}'")->update($data);
	        if($ret){
	            return $this->renderSuccess([],'保存成功');
	        }
	        return $this->renderError('保存失败');
		}		 
        $data  = db('admin_menu')->where(['id'=>input('id')])->find();  
		$lists = db('admin_menu')->where(['status'=>1])->select();
		$lists = self::getMenuList($lists);
		return $this->fetch('edit',[ 'data'=>$data, 'lists'=>$lists]);
    }   

}
